package com.example.myfirstapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONArray;
import org.json.JSONObject;

import com.example.myfirstapp.R;
import com.example.myfirstapp.table.ScrollingTable;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class firstActiviti extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//       setContentView(com.edgar.R.layout.main);
//       TableLayout header = (TableLayout)findViewById(R.id.BodyTable);
//       TableRow tr= new TableRow(this);
//       CheckBox cb = new CheckBox(this);
//       CheckBox cb2 = new CheckBox(this);
//       //cb.sett
//       tr.addView(cb);
//       tr.addView(cb2);
//       header.addView(tr);
        try{
        //json object whit file structure (for test it is generated from a file in assests)
		InputStream stt = this.getAssets().open("test3.txt");
		BufferedReader in  = new BufferedReader(new InputStreamReader(stt,"UTF-8"));
		String str = in.readLine(); //str will be get from network in the future
		JSONObject fileJSO = new JSONObject(str); // this object is input for next part of process 
		
		JSONObject fil = fileJSO.getJSONObject("file");
		
		JSONObject row = fil.getJSONObject("row");
		JSONArray rowarray = row.getJSONArray("rowitem"); // get row array
		JSONObject column = fil.getJSONObject("column");
		JSONArray columnarray = column.getJSONArray("columnitem"); // get column array		
		JSONObject value = fil.getJSONObject("value");
		JSONArray valuearray = value.getJSONArray("valueitem"); // get value array		
		
        // inflation layout and get table layout for add rows
        String infService = Context.LAYOUT_INFLATER_SERVICE;
    	LayoutInflater li;
    	li = (LayoutInflater) this.getSystemService(infService);
    	LinearLayout h= new LinearLayout(this);
    	li.inflate(R.layout.main, h);
    	LinearLayout hr = (LinearLayout)h.getChildAt(0);
    	ScrollingTable st = (ScrollingTable)hr.getChildAt(0);
    	ScrollView tb = (ScrollView)st.getChildAt(1);
    	TableLayout tbl = (TableLayout)tb.getChildAt(0);

    	
    	//creation components to add in table layout
		for (int i = 0; i < rowarray.length(); i++) { 
		    // create rows and text in first column in each row
	    	TableRow trw = new TableRow(this);
	    	li.inflate(R.layout.row,trw);
	    	trw.removeAllViews();
	    	EditText ed = new EditText(this);
	    	ed.setText(""+rowarray.getJSONObject(i).get("name"));
	    	trw.addView(ed);
			System.out.println("INTERNO "+rowarray.getJSONObject(i).get("name"));
			for (int r = 0; r < columnarray.length(); r++) { 
			    // create components for each column in the row
//				View  chb = View.inflate(this, R.layout.check, null);
				EdgarCheckBox  chb = (EdgarCheckBox)View.inflate(this, R.layout.check, null);
				chb.setText(""+columnarray.getJSONObject(r).get("type"));
				chb.setIdsColumn(""+columnarray.getJSONObject(r).get("ids"));
				chb.setType(""+columnarray.getJSONObject(r).get("type"));
				chb.setIdsRow(""+rowarray.getJSONObject(i).get("ids"));
				chb.setValue(""+columnarray.getJSONObject(r).get("default"));
				trw.addView(chb);
				System.out.println("INTERNO "+columnarray.getJSONObject(r).get("default"));
				//trw.addView(chb);
			}
			tbl.addView(trw);
		}
		
    	TableRow trw = new TableRow(this);
    	li.inflate(R.layout.row,trw);
    	    //CheckBox chb = (CheckBox)this.CheckBox(this);
    		trw.removeAllViews();
    	    CheckBox  chb = (CheckBox)View.inflate(this, R.layout.check, null);
    	    System.out.println("##$$$###$$"+chb.getText());
    		trw.addView(chb);
    		CheckBox  chb2 = (CheckBox)View.inflate(this, R.layout.check, null);//((ViewGroup)trw));
    		trw.addView(chb2);	
    	tbl.addView(trw);
    	
    	
    	TableRow trw2 = new TableRow(this);
    	li.inflate(R.layout.row,trw2);
	    	CheckBox  chb3 = (CheckBox)View.inflate(this, R.layout.check, null);
	    	trw2.addView(chb3);
    	tbl.addView(trw2);
    	
    	setContentView(h);
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }
        
    }
}